<template>
<div>
  <jxf-titletip :title="title"></jxf-titletip>
  <div class="ruleSet mt20" >
    <el-form :model="ruleSetInfo" ref="ruleSetInfo" :inline="false" label-width="180px" :rules="rules">
      <el-form-item label="订单完结后结算时间:" required prop="settleDuration">
        <el-input v-model="ruleSetInfo.settleDuration" onkeyup="value=value.replace(/[^\d]/g,'')">
          <template slot="append">（h）</template>
        </el-input>
      </el-form-item>
      <el-form-item label="供应商平台信息费率:" required prop="infoFee">
        <el-input v-model="ruleSetInfo.infoFee">
          <template slot="append">（%）</template>
        </el-input>
      </el-form-item>
      <div class="ruleSetDetail">
        <div class="ruleTitle">提现规则</div>
        <el-row :gutter="10" class="pd10">
          <el-col :xs="24" :sm="24" :md="24" :lg="2" :xl="2"><span class="setting-obj-title">规则：</span></el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="22" :xl="22">
            <el-col :xs="24" :sm="24" :md="24" :lg="8" :xl="8">
              <el-form-item label="单笔最低:" class="setting-input" required prop="onceMinAmount">
                <el-input v-model="ruleSetInfo.onceMinAmount" onkeyup="this.value= this.value.match(/\d+(\.\d{0,2})?/) ? this.value.match(/\d+(\.\d{0,2})?/)[0] : ''">
                  <template slot="append">元</template>
                </el-input>
              </el-form-item>
              <el-form-item label="单笔最高:" class="setting-input" required prop="onceMaxAmount">
                <el-input v-model="ruleSetInfo.onceMaxAmount" onkeyup="this.value= this.value.match(/\d+(\.\d{0,2})?/) ? this.value.match(/\d+(\.\d{0,2})?/)[0] : ''">
                  <template slot="append">元</template>
                </el-input>
              </el-form-item>
              <el-form-item label="单日最高:" class="setting-input" required prop="dayMaxAmount">
                <el-input v-model="ruleSetInfo.dayMaxAmount" onkeyup="this.value= this.value.match(/\d+(\.\d{0,2})?/) ? this.value.match(/\d+(\.\d{0,2})?/)[0] : ''">
                  <template slot="append">元</template>
                </el-input>
              </el-form-item>
              <el-form-item label="单日最高提现次数:" class="setting-input" required prop="dayTimes">
                <el-input v-model="ruleSetInfo.dayTimes" onkeyup="value=value.replace(/[^\d]/g,'')">
                  <template slot="append">次</template>
                </el-input>
              </el-form-item>
              <el-form-item label="单笔手续费:" class="setting-input" required prop="onceHandlingFee">
                <el-input v-model="ruleSetInfo.onceHandlingFee" onkeyup="value=value.replace(/[^\d]/g,'')">
                  <template slot="append">元</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="24" :md="24" :lg="{ span: 14, offset: 2  }" :xl="{ span: 14, offset: 2  }">
              <el-form-item label="设置提现状态及时间:"required>
                <el-time-select
                  placeholder="起始时间"
                  v-model="ruleSetInfo.startTime"
                  :picker-options="{
                      start: '00:00',
                      step: '00:15',
                      end: '23:44'
                    }">
                </el-time-select>
                <el-time-select
                  placeholder="结束时间"
                  v-model="ruleSetInfo.endTime"
                  :picker-options="{
                    start: '00:15',
                    step: '00:15',
                    end: '23:59',
                    minTime: ruleSetInfo.startTime
                  }">
                  <el-input v-model="ruleSetInfo.time" style="display: none"></el-input>
                </el-time-select>
              </el-form-item>
              <el-form-item label="提现规则说明(用户可见):">
                <el-input type="textarea" :rows="5" placeholder="请输入内容" v-model="ruleSetInfo.description">
                </el-input>
              </el-form-item>
            </el-col>
          </el-col>
        </el-row>
      </div>
      <div class="pd10">
        <el-button type="primary" @click="saveRuleSet">保存</el-button>
      </div>
    </el-form>
  </div>
</div>
</template>

<script>
    export default {
        name: "AccountRuleSet",
        data() {
          var time = (rule, value, callback) => {
            if(!this.ruleSetInfo.startTime) {
              return callback("开始时间不能为空")
            } else if(!this.ruleSetInfo.endTime) {
              return callback("结束时间不能为空")
            } else {
              return callback()
            }
          }
          return {
            title: "财务规则设置",
            ruleSetInfo: {
              infoFee: "",
              settleDuration: "",
              dayMaxAmount: "",
              dayTimes: "",
              description: "",
              endTime: "",
              onceHandlingFee: "",
              onceMaxAmount: "",
              onceMinAmount: '',
              startTime: "",
              time: "11"
            },
            rules: {
              settleDuration: [
                { validator: validate.max1000, trigger: ['change', 'blur'] },
                { required: true, message: '完结后的订单时间不能为空', trigger: ['change', 'blur'] },
              ],
              infoFee: [
                { validator: validate.feeValidate, trigger: ['change', 'blur'] },
                { required: true, message: '信息服务率不能为空', trigger: [ 'change', 'blur'] },
              ],
              time: [
                { validator: time, trigger: ['change', 'blur'] },
                { required: true, message: '提现状态及时间', trigger: ['change', 'blur'] },
              ],
              onceMinAmount: [
                { required: true, message: '单笔最低不能为空', trigger: ['change', 'blur'] },
              ],
              onceMaxAmount: [
                { required: true, message: '单笔最高不能为空', trigger: ['change', 'blur'] },
              ],
              dayMaxAmount: [
                { required: true, message: '单日最高不能为空', trigger: ['change', 'blur'] },
              ],
              dayTimes: [
                { required: true, message: '单日最高提现次数不能为空', trigger: ['change', 'blur'] },
              ],
              onceHandlingFee: [
                { required: true, message: '单笔手续费不能为空', trigger: ['change', 'blur'] },
              ]
            }
          }
        },
        methods: {
          getData() {
            ajax.get(apis.finance.getSettingInfo).then(result => {
              if(result.code === "10000000") {
                this.ruleSetInfo = {
                  infoFee: result.data.infoFee !== null && result.data.infoFee !== "" ? Number(result.data.infoFee) * 10000 / 100 : "",
                  settleDuration: Number(result.data.settleDuration),
                  dayMaxAmount: result.data.withdrawPolicy.dayMaxAmount,
                  endTime: result.data.withdrawPolicy.endTime,
                  startTime: result.data.withdrawPolicy.startTime,
                  onceMaxAmount: result.data.withdrawPolicy.onceMaxAmount,
                  onceMinAmount: result.data.withdrawPolicy.onceMinAmount,
                  dayTimes: result.data.withdrawPolicy.dayTimes,
                  description: result.data.withdrawPolicy.description,
                  onceHandlingFee: result.data.withdrawPolicy.onceHandlingFee,
                }
              } else {
                this.$message.error(result.message)
              }
            })
          },
          saveRuleSet() {
            let data = {
              infoFee: this.ruleSetInfo.infoFee!== "" ? (Number(this.ruleSetInfo.infoFee)  * 100/ 10000).toString() : "",
              settleDuration: this.ruleSetInfo.settleDuration,
              withdrawPolicy: {
                dayMaxAmount: this.ruleSetInfo.dayMaxAmount,
                dayTimes: this.ruleSetInfo.dayTimes,
                description: this.ruleSetInfo.description,
                endTime: this.ruleSetInfo.endTime,
                onceHandlingFee: this.ruleSetInfo.onceHandlingFee,
                onceMaxAmount: this.ruleSetInfo.onceMaxAmount,
                onceMinAmount: this.ruleSetInfo.onceMinAmount,
                startTime: this.ruleSetInfo.startTime
              }
            }
            this.$refs['ruleSetInfo'].validate((valid) => {
              if (valid) {
                ajax.post(apis.finance.saveSetting, data).then(result => {
                  if(result.code === "10000000") {
                    this.$message({
                      type: 'success',
                      message: '保存成功!'
                    });
                    this.getData()
                  } else {
                    this.$message.error(result.message)
                  }
                })
              }})

          }
        },
        created() {
          this.getData()
        }
    }
</script>

<style lang="scss" scoped>
  .ruleSet {
    padding: 20px;
    background: #fff;
    border-radius: 5px;
    .el-input {
      width: 260px;
    }
    .ruleTitle {
      padding: 10px;
      height: 40px;
      line-height: 40px;
      border-radius: 4px;
      background: #f6f6f6;
    }
    .setting-obj-title {
      padding-left: 10px;
    }
    .ruleSetDetail {

    }

  }
</style>
